From 50a0f0240d7fef133eb5acc1bea2b1168b08e9db Mon Sep 17 00:00:00 2001 From: Factiven Date: Sun, 24 Dec 2023 13:03:54 +0700 Subject: migrate to typescript --- pages/en/search/[...param].tsx | 598 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 598 insertions(+) create mode 100644 pages/en/search/[...param].tsx (limited to 'pages/en/search/[...param].tsx') diff --git a/pages/en/search/[...param].tsx b/pages/en/search/[...param].tsx new file mode 100644 index 0000000..5a34ff5 --- /dev/null +++ b/pages/en/search/[...param].tsx @@ -0,0 +1,598 @@ +import { Key, useEffect, useRef, useState } from "react"; +import { motion as m } from "framer-motion"; +import Skeleton from "react-loading-skeleton"; +import { useRouter } from "next/router"; +import Link from "next/link"; +import Head from "next/head"; +import Footer from "@/components/shared/footer"; + +import Image from "next/image"; +import { aniAdvanceSearch } from "@/lib/anilist/aniAdvanceSearch"; +import MultiSelector from "@/components/search/dropdown/multiSelector"; +import SingleSelector from "@/components/search/dropdown/singleSelector"; +import { + animeFormatOptions, + formatOptions, + genreOptions, + mangaFormatOptions, + mediaType, + seasonOptions, + tagsOption, + yearOptions, +} from "@/components/search/selection"; +import InputSelect from "@/components/search/dropdown/inputSelect"; +import { Cog6ToothIcon, TrashIcon } from "@heroicons/react/20/solid"; +import useDebounce from "@/lib/hooks/useDebounce"; +import { Navbar } from "@/components/shared/NavBar"; +import MobileNav from "@/components/shared/MobileNav"; +import SearchByImage, { + TraceMoeResultTypes, +} from "@/components/search/searchByImage"; +import { PlayIcon } from "@heroicons/react/24/outline"; +import { StaticImport } from "next/dist/shared/lib/get-img-props"; + +export async function getServerSideProps(context: any) { + const { param } = context.query; + + const { search, format, genres, season, year } = context.query; + + let getFormat, getSeason, getYear; + let getGenres = []; + + if (genres) { + const gr = genreOptions.find( + (i) => i.value.toLowerCase() === genres.toLowerCase() + ); + getGenres.push(gr); + } + + if (season) { + getSeason = seasonOptions.find( + (i) => i.value.toLowerCase() === season.toLowerCase() + ); + if (!year) { + const now = new Date().getFullYear(); + getYear = yearOptions.find((i) => i.value === now.toString()); + } else { + getYear = yearOptions.find((i) => i.value === year); + } + } + + if (format) { + getFormat = formatOptions.find( + (i) => i.value.toLowerCase() === format.toLowerCase() + ); + } + + if (!param && param.length !== 1) { + return { + notFound: true, + }; + } + + const typeIndex = param[0] === "anime" ? 0 : 1; + + return { + props: { + index: typeIndex, + query: search || null, + formats: getFormat || null, + seasons: getSeason || null, + years: getYear || null, + genres: getGenres || null, + }, + }; +} + +type CardProps = { + index: number; + query: string; + genres: any; + formats: any; + seasons: any; + years: any; +}; + +export default function Card({ + index, + query, + genres, + formats, + seasons, + years, +}: CardProps) { + const inputRef = useRef(null); + const router = useRouter(); + + const [data, setData] = useState(); + const [imageSearch, setImageSearch] = useState(); + + const [loading, setLoading] = useState(true); + + const [search, setQuery] = useState(query); + const debounceSearch = useDebounce(search, 500); + + const [type, setSelectedType] = useState<{ + name: string; + value: string; + } | null>(mediaType[index]); + const [year, setYear] = useState(years); + const [season, setSeason] = useState(seasons); + const [sort, setSelectedSort] = useState<{ name: string; value: string }>(); + const [genre, setGenre] = useState(genres); + const [format, setFormat] = useState(formats); + + const [isVisible, setIsVisible] = useState(false); + + const [page, setPage] = useState(1); + const [nextPage, setNextPage] = useState(true); + + async function advance() { + setLoading(true); + const data = await aniAdvanceSearch({ + search: debounceSearch, + type: type?.value as "ANIME" | "MANGA" | undefined, + genres: genre, + page: page, + sort: sort?.value, + format: format?.value, + season: season?.value, + seasonYear: year?.value, + }); + if (data?.media?.length === 0) { + setNextPage(false); + setLoading(false); + } else if (data !== null && page > 1) { + setData((prevData: any) => { + return [...(prevData ?? []), ...data?.media]; + }); + setNextPage(data?.pageInfo.hasNextPage); + setLoading(false); + } else { + setData(data?.media); + setNextPage(data?.pageInfo.hasNextPage); + setLoading(false); + } + } + + useEffect(() => { + setData(null); + setPage(1); + setNextPage(true); + if (page === 1) { + advance(); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [ + debounceSearch, + type?.value, + sort?.value, + genre, + format?.value, + season?.value, + year?.value, + ]); + + useEffect(() => { + if (imageSearch) return; + if (page > 1) { + advance(); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [page, imageSearch]); + + useEffect(() => { + function handleScroll() { + if (imageSearch) { + window.removeEventListener("scroll", handleScroll); + return; + } + if (page > 10 || !nextPage) { + window.removeEventListener("scroll", handleScroll); + return; + } + + if ( + window.innerHeight + window.pageYOffset >= + document.body.offsetHeight - 3 + ) { + if (!loading) { + setPage((prevPage) => prevPage + 1); + } + } + } + + window.addEventListener("scroll", handleScroll); + + return () => window.removeEventListener("scroll", handleScroll); + }, [page, nextPage, imageSearch, loading]); + + const handleKeyDown = async (event: any) => { + if (event.key === "Enter") { + event.preventDefault(); + const inputValue = event.target.value; + if (inputValue === "") { + setQuery(undefined); + } else { + setQuery(inputValue); + } + } + }; + + function trash() { + setImageSearch(undefined); + setQuery(undefined); + setGenre(undefined); + setFormat(undefined); + setSelectedSort(undefined); + setSeason(undefined); + setYear(undefined); + router.push(`/en/search/${mediaType[index]?.value?.toLowerCase()}`); + } + + function handleVisible() { + setIsVisible(!isVisible); + } + + const handleVideoHover = (hovered: boolean, id: any) => { + const updatedImageSearch = imageSearch?.map((item: any) => { + if (item.filename === id) { + return { ...item, hovered }; + } + return item; + }); + setImageSearch(updatedImageSearch); + }; + + // console.log({ loading, data }); + + return ( + <> + + Moopa - search + + + + + + + +
+
+
+
+ + {/* GENRES */} + + {/* SORT */} + {/* */} + {/* FORMAT */} + + {/* SEASON */} + + {/* YEAR */} + +
+
+ +
+ +
+
+ +
+ +
+ +
+
+
+ {isVisible && ( +
+
+ {/* GENRES */} + + {/* SORT */} + {/* */} + {/* FORMAT */} + + {/* SEASON */} + + {/* YEAR */} + +
+
+ )} + {/*
*/} +
+
+ {loading + ? "" + : !data && ( +
+ Oops!

Nothing's Found... +
+ )} + + {data && + data?.length > 0 && + !imageSearch && + data?.map( + ( + anime: { + format: string; + id: any; + title: { userPreferred: string }; + coverImage: { extraLarge: string | StaticImport }; + status: string; + episodes: any; + chapters: any; + }, + index: Key | null | undefined + ) => { + return ( + + + {anime.title.userPreferred} + + +

+ {anime.status === "RELEASING" ? ( + + ) : anime.status === "NOT_YET_RELEASED" ? ( + + ) : null} + {anime.title.userPreferred} +

+ +

+ {anime.format ||

-

} ·{" "} + {anime.status ||

-

} ·{" "} + {anime.episodes + ? `${anime.episodes || "N/A"} Episodes` + : `${anime.chapters || "N/A"} Chapters`} +

+
+ ); + } + )} + + {loading && ( + <> + {[1, 2, 4, 5, 6, 7, 8].map((item) => ( +
+
+ +
+
+

+ +

+
+
+ ))} + + )} +
+ + {imageSearch && ( +
+ {imageSearch.map((a, index) => { + return ( + + { + handleVideoHover(true, a.filename); + }} + onMouseLeave={() => handleVideoHover(false, a.filename)} + > +
+
+ +

+ {`Episode ${a.episode}`} +

+
+ + {a?.image && ( + Episode Thumbnail + )} + {a?.video && ( +
+ )} + {!loading && page > 10 && nextPage && ( + + )} +
+ {/*
*/} +
+
+